/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MeusBlogs.java
 *
 * Created on 27/11/2010, 22:44:49
 */

package guiDesktop;


import br.edu.ufcg.dsc.si.blog.webservice.BlogWSImpl;
import classes.Blog;
import facades.FacadeBlog;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Tiago
 */
public class MeusBlogs extends javax.swing.JFrame implements KeyListener {
    /** Creates new form MeusBlogs */
    private String idSessao;
    BlogWSImpl fachada = new BlogWSImpl();

    @SuppressWarnings("deprecation")
    @Override
    public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    botaoCadastrarActionPerformed(null);

            } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    botaoCancelarActionPerformed(null);
            }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub

    }

    @Override
    public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub

    }
    public MeusBlogs(String idSessao) {

        initComponents();

        this.idSessao = idSessao;
        this.addKeyListener(this);
        this.show();


        jListBlogs.addKeyListener(this);
        jListBlogs.show();


        setVisible(true);
        setExtendedState(MAXIMIZED_BOTH);

        reiniciaCampos();

        try {
           buscaBlogs();
        } catch (Exception ex) {
           JOptionPane.showMessageDialog(null, ex.getMessage(),
                "Busca Sem Sucesso",
                JOptionPane.INFORMATION_MESSAGE);
        }


    }

    private void buscaBlogs() throws Exception {
        final FacadeBlog fachada = FacadeBlog.getInstance();
    	if(idSessao != null) {
            jListBlogs.setModel(new javax.swing.AbstractListModel() {
            List<Blog> clientes = fachada.getBlogsBySessionId(idSessao);
            public int getSize() {return clientes.size();}
            public String getElementAt(int i){return clientes.get(i).getId();}
            });
            jScrollPane1.setViewportView(jListBlogs);
        } else
            throw new Exception("ID da sessão inválida");
    }

     private void reiniciaCampos() {
        jListBlogs.setModel(new javax.swing.AbstractListModel() {
            Blog[] blogs = {};
            public int getSize() {return blogs.length;}
            public Blog getElementAt(int i){return blogs[i];}
        });



    }
     

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jListBlogs = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        botaoCadastrar = new javax.swing.JButton();
        botaoCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jScrollPane1.setViewportView(jListBlogs);

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 24)); // NOI18N
        jLabel2.setText("Blogs");

        botaoCadastrar.setText("Acessar");
        botaoCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCadastrarActionPerformed(evt);
            }
        });

        botaoCancelar.setText("Voltar");
        botaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(169, 169, 169)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botaoCancelar)
                        .addGap(18, 18, 18)
                        .addComponent(botaoCadastrar))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 424, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(135, 135, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel2)
                .addGap(37, 37, 37)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoCadastrar)
                    .addComponent(botaoCancelar))
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCadastrarActionPerformed
         if(jListBlogs.isSelectionEmpty())
            JOptionPane.showMessageDialog(null, "Nenhum Blog foi selecionado",
                "Blog",
                JOptionPane.ERROR_MESSAGE);
        else {
            String blog = (String)jListBlogs.getSelectedValue();
            this.dispose();
            new MenuBlog(blog, idSessao);
            
        }
    }//GEN-LAST:event_botaoCadastrarActionPerformed

    private void botaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCancelarActionPerformed
        this.dispose();
        new FramePrincipal(idSessao);
}//GEN-LAST:event_botaoCancelarActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        try {
            fachada.logoff(idSessao);
            fachada.saveData();
        } catch (Exception ex) {
           JOptionPane.showMessageDialog(null, "Logoff sem sucesso!",
                "Logoff sem sucesso!",
                JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_formWindowClosing



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoCadastrar;
    private javax.swing.JButton botaoCancelar;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jListBlogs;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

}
